<html>
<head><meta charset="utf-8"><title>Querying the layout/ABI of types (not just size+align) · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html">Querying the layout/ABI of types (not just size+align)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211274266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211274266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211274266">(Sep 25 2020 at 15:35)</a>:</h4>
<p>Hi,</p>
<p>I was wondering if there is a way to query the ABI of (for example) structs from the user Rust program? In addition to what I can already get from <code>std::mem::align_of</code>, I'm specifically interested in the offsets of fields and the order they've been codegenned.</p>
<p>So far I'm using this home-grown solution:<br>
<a href="https://gist.github.com/vext01/536123426f8c1141c158bdf49c4efff7">https://gist.github.com/vext01/536123426f8c1141c158bdf49c4efff7</a></p>
<p>Is there a better way? Perhaps without requiring instantiation of the type in question?</p>
<p>Thanks.</p>



<a name="211274603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211274603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211274603">(Sep 25 2020 at 15:38)</a>:</h4>
<p>I remember the Rust ABI being explicitly unstable to allow for optimisations</p>



<a name="211274625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211274625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211274625">(Sep 25 2020 at 15:38)</a>:</h4>
<p>There's built-in nightly attribute, on phone now so I can't check but its name maybe rustc_layout.</p>



<a name="211274765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211274765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211274765">(Sep 25 2020 at 15:39)</a>:</h4>
<p>You can also try to use <a href="https://doc.rust-lang.org/stable/std/alloc/struct.Layout.html">https://doc.rust-lang.org/stable/std/alloc/struct.Layout.html</a> but that’s not its intended use at all I believe</p>



<a name="211275022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211275022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211275022">(Sep 25 2020 at 15:41)</a>:</h4>
<p><span class="user-mention" data-user-id="209117">@lzutao</span>  <a href="https://www.ralfj.de/blog/2020/04/04/layout-debugging.html">https://www.ralfj.de/blog/2020/04/04/layout-debugging.html</a> -- thanks!</p>
<p><span class="user-mention" data-user-id="307289">@Poliorcetics</span> I'm aware of that one. It only exposes size and alignment, not field offsets or field order.</p>
<p>The one <span class="user-mention" data-user-id="209117">@lzutao</span> posted looks good.</p>



<a name="211314896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211314896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211314896">(Sep 25 2020 at 20:36)</a>:</h4>
<p>Layout.extend can  to get the layout of <strong>repr(C)</strong>  structs by "recreating it" (e.g. automatically using a custom derive).</p>
<p>But for the rust ABI there is currently no way to get the layout (on stable, besides size+ alignment).</p>



<a name="211315048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211315048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211315048">(Sep 25 2020 at 20:37)</a>:</h4>
<p>TIL <code>Layout::extend</code> is a thing (and it's even stable!)</p>



<a name="211315490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211315490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211315490">(Sep 25 2020 at 20:41)</a>:</h4>
<p>(but repr(C) only)</p>
<p>For non repr(C) Best you can probably could do is crate a instance of the struct, crate field references to pointer arithmetic and store that in a lazy static. Furthermore autogenerated that using a custom derive or similar.</p>
<p>In both cases you can only do so for structs where you (as a person) or a custom derive know of the structs fields.</p>



<a name="211315618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211315618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211315618">(Sep 25 2020 at 20:42)</a>:</h4>
<p>Also preferably only do unsafe magic with <code>repr(C)</code> types for now.</p>



<a name="211316403"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/211316403" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#211316403">(Sep 25 2020 at 20:50)</a>:</h4>
<p>Oh and be aware that composed DST structs (structs which last field is a DST) the padding between the last non-DST field and the tailing DST field does depend on the pointer metadata if the DST is based on <code>dyn Trait</code>. It's not a problem if it's based on <code>[T]</code> or <code>str</code>. </p>
<p>Through that doesn't matter if you only work with sized types for whatever you want to do.</p>



<a name="221567136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/221567136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ember arlynx <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#221567136">(Jan 04 2021 at 19:14)</a>:</h4>
<p><span class="user-mention" data-user-id="223288">@Edd Barrett</span>  it is good timing that you ask this because this weekend i started working on a #[derive(Reflect)] that does exactly what <span class="user-mention" data-user-id="209646">@Philipp Korber</span> describes</p>



<a name="221567223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/221567223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ember arlynx <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#221567223">(Jan 04 2021 at 19:15)</a>:</h4>
<p>oh, i can't read, the google search result that showed me this msg was indexed a day ago or something, this thread is old af.</p>



<a name="221572306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Querying%20the%20layout/ABI%20of%20types%20%28not%20just%20size%2Balign%29/near/221572306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Querying.20the.20layout.2FABI.20of.20types.20(not.20just.20size.2Balign).html#221572306">(Jan 04 2021 at 19:55)</a>:</h4>
<p>I'm still interested :) is there an RFC?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>